﻿using CleverDD.Model.Param;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrameWorkDemo.UC
{
    public partial class BasicParamUc : UserControl
    {
       


        public BasicParamUc()
        {
            InitializeComponent();
        }


        private TableLayoutPanel _mainTableLayoutPanel = new TableLayoutPanel();

        public List<ParamInfo> ParamList;
        public int Column1WidthPercent = 50;
        public int Column2WidthPercent = 50;

        #region 初始化绘制页面

        public void InitControl(List<ParamInfo> paramList)
        {

            ParamList = paramList;

            int rowCount = ParamList.Count;


            _mainTableLayoutPanel.Location = new Point(5, 5);
            _mainTableLayoutPanel.RowCount = rowCount;
            _mainTableLayoutPanel.ColumnCount = 2; // 设置列数
            _mainTableLayoutPanel.Dock = DockStyle.Fill;
            //第一列宽度
            _mainTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, Column1WidthPercent));
            //第二列宽度
            _mainTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, Column2WidthPercent));

           

            //等分行高
            int[] rowPercent = DivideItemsEqually(_mainTableLayoutPanel.RowCount);
            foreach (var item in rowPercent)
            {
                _mainTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, item));
            }

            //显示网格线
            _mainTableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            //添加参数控件
            for (int i = 0; i < ParamList.Count; i++)
            {
                // 第一个参数是列索引，第二个参数是行索引
                _mainTableLayoutPanel.Controls.Add(GetLabel(ParamList[i]), 0, i);
                GetLabel(ParamList[i]).Anchor = AnchorStyles.Right;
                switch (ParamList[i].ParamType)
                {
                    case ParamType.ComboBox:
                        _mainTableLayoutPanel.Controls.Add(GetComboBox(ParamList[i]), 1, i);
                        break;
                    case ParamType.TextBox:
                        _mainTableLayoutPanel.Controls.Add(GetTextBox(ParamList[i]), 1, i);
                        break;
                    case ParamType.YesOrNo:
                        _mainTableLayoutPanel.Controls.Add(GetYesOrNoTableLayoutPanel(ParamList[i]), 1, i);
                        break;
                }
            }

        

            this.Controls.Add(_mainTableLayoutPanel);

        }



        public void InitData(List<string> dataList)
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                Control control = _mainTableLayoutPanel.GetControlFromPosition(1, i); // 列索引从0开始，所以第2列的索引是1

                if (control != null)
                {
                    switch (control)
                    {
                        case TextBox _:
                        case ComboBox _:
                            control.Text = dataList[i];
                            break;
                        case TableLayoutPanel tableLayoutPanel:
                            foreach (Control radioControl in tableLayoutPanel.Controls)
                            {
                                bool.TryParse(dataList[i], out bool yes);

                                if (radioControl is RadioButton radioButton)
                                {
                                    if (radioButton.Name.Contains("Yes"))
                                    {
                                        radioButton.Checked = yes;
                                        break;
                                    }

                                    if (radioButton.Name.Contains("No"))
                                    {
                                        radioButton.Checked = !yes;
                                        break;
                                    }
                                }
                            }
                            break;
                    }
                }
            }
        }

        public int[] DivideItemsEqually(int count)
        {
            // 参数验证：确保 N 是正数并且不超过100
            if (count <= 0 || count > 100)
            {
                return Array.Empty<int>();
            }

            // 创建结果数组
            int[] result = new int[count];

            // 计算每份的基础数量和剩余数量
            int baseCount = 100 / count;  // 每份的基本数量
            int remainder = 100 % count;  // 剩余的数量

            // 分配基础数量到每一组
            for (int i = 0; i < count; i++)
            {
                result[i] = baseCount;
            }

            // 将剩余物品分配给最后一份
            if (remainder > 0)
            {
                result[count - 1] += remainder;
            }

            return result;
        }

        #endregion

        #region  生成不同控件
        public Label GetLabel(ParamInfo paramInfo)
        {

            Label label = new Label();
            label.Text = paramInfo.LabelName + ":";
            label.AutoSize = false;
            //label.Size = new Size(Width*Column1WidthPercent/100 - 5, 36); // 设置固定大小
            label.Anchor = AnchorStyles.Right;
            label.Font = new Font(label.Font.FontFamily, 12, label.Font.Style);
            label.TextAlign = ContentAlignment.MiddleRight;
            return label;
        }

        public TextBox GetTextBox(ParamInfo paramInfo)
        {
            TextBox textBox = new TextBox();
            textBox.Text = paramInfo.OldValue;
            textBox.Size = new Size(Width * Column2WidthPercent / 100  - 10, 36); // 设置固定大小
            textBox.Anchor = AnchorStyles.Left;
            textBox.Font = new Font(textBox.Font.FontFamily, 12, textBox.Font.Style);
            textBox.TextAlign = HorizontalAlignment.Left;
            return textBox;
        }

        public ComboBox GetComboBox(ParamInfo paramInfo)
        {
            ComboBox comboBox = new ComboBox();
            comboBox.Text = paramInfo.OldValue;
            comboBox.Size = new Size(Width * Column2WidthPercent / 100 - 5, 36); // 设置固定大小
            comboBox.Anchor = AnchorStyles.Left;
            comboBox.Font = new Font(comboBox.Font.FontFamily, 12, comboBox.Font.Style);
            foreach (string item in paramInfo.ValueList)
            {
                comboBox.Items.Add(item);
            }

            return comboBox;
        }

        public TableLayoutPanel GetYesOrNoTableLayoutPanel(ParamInfo paramInfo)
        {
            TableLayoutPanel tableLayoutPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 2,
                RowCount = 1,
                Padding = new Padding(0), // 可选：添加一些内边距
                Margin = new Padding(0),
            };
            RadioButton radioButtonYes = new RadioButton();
            RadioButton radioButtonNo = new RadioButton();

            radioButtonYes.Font = new Font(radioButtonYes.Font.FontFamily, 10, radioButtonYes.Font.Style);
            radioButtonNo.Font = new Font(radioButtonYes.Font.FontFamily, 10, radioButtonYes.Font.Style);

            // 初始化 Panel
            tableLayoutPanel.Dock = DockStyle.Fill;

            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
            //第二列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));

            // 初始化 "是" 单选框
            radioButtonYes.Text = "是";
            radioButtonYes.Name = "Yes";
            radioButtonYes.Anchor = AnchorStyles.None;
            radioButtonYes.Size = new Size(40, 25);
            // 初始化 "否" 单选框
            radioButtonNo.Text = "否";
            radioButtonNo.Name = "No";
            radioButtonNo.Anchor = AnchorStyles.None;
            radioButtonNo.Size = new Size(40, 25);



            tableLayoutPanel.Controls.Add(radioButtonYes, 0, 0);
            tableLayoutPanel.Controls.Add(radioButtonNo, 1,0);
            bool.TryParse(paramInfo.OldValue, out bool flag);

            radioButtonYes.Checked = flag;
            radioButtonNo.Checked = !flag;

            return tableLayoutPanel;
        }


        #endregion


        #region 获取页面上的值
        public void GetAllValue()
        {
            for (var row = 0; row < ParamList.Count; row++)
            {
                Control control = _mainTableLayoutPanel.GetControlFromPosition(1, row); // 列索引从0开始，所以第2列的索引是1
                if (control != null)
                {
                    if (control is TextBox textBox)
                    {
                        ParamList[row].NewValue = textBox.Text.Trim();
                    }

                    if (control is ComboBox comboBox)
                    {
                        ParamList[row].NewValue = comboBox.Text.Trim();
                    }

                    if (control is TableLayoutPanel tableLayoutPanel)
                    {
                        ParamList[row].NewValue = "false";
                        foreach (Control radioControl in tableLayoutPanel.Controls)
                        {
                            // 你可以在这里对找到的控件进行操作
                            if (radioControl is RadioButton radioButton)
                            {
                                if (radioButton.Name.Contains("Yes") && radioButton.Checked)
                                {
                                    ParamList[row].NewValue = "true";
                                    break;
                                }

                            }

                        }
                    }

                }
            }
        }


        #endregion


    }
}
